package day07.ThreadDemo;

/*
 * Author: JacksonCoder
 * FileName: SemaphoreDemo
 * Version: 1.0.0
 * Date: 2025/4/14
 */

import java.util.concurrent.Semaphore;

public class SemaphoreDemo {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2); // 同时允许两个线程

        for (int i = 1; i <= 10; i++) {
            int id = i;
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println("Thread " + id + " acquired");
                    Thread.sleep(1000);
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

}
